<?php
require_once 'NotesView.php';
require_once 'Note.php';

class NotesHandler
{
    private $m_database = NULL;
	private $m_tablePrefix = NULL;

    public function __construct(DatabaseConnection $database)
    {
        $this->m_database = $database;
		$this->m_tablePrefix = $database->getTablePrefix();
    }
	
	// #### CREATE METODER #### //
	
	// Lägga till en note.
    public function addNote($noteName, $noteContent, $noteDateTime, $uid)
    {   
        $sqlStatement = "INSERT INTO ".$this->m_tablePrefix."notes (notename, notecontent, notedatetime, uid) VALUES  (?, ?, ?, ?)";

        if ($stmt = $this->m_database->PrepareStatement($sqlStatement))
        {

            $stmt->bind_param("sssi", $noteName, $noteContent, $noteDateTime, $uid);

            $stmt->execute();

            $stmt->close();
            
            return true;
            }
            
        return false;
    }
		
	// #### READ METODER #### //
	
	// Hämta användarens alla notes.
    public function getNotes($userId)
    {
        $NotesArray = array();
        
        $sqlStatement = "SELECT nid, notename, notecontent, notedatetime FROM ".$this->m_tablePrefix."notes WHERE uid=? ORDER BY nid DESC";

        $stmt = $this->m_database->PrepareStatement($sqlStatement);

        $stmt->bind_param("i", $userId);

        $stmt->execute();

        $stmt->bind_result($nid, $notename, $notecontent, $notedatetime);

        while ($stmt->fetch())
        {
			$note = new Note($nid, $notename, $notecontent, $notedatetime);
            $NotesArray[] = $note;
        }

        $stmt->close();

        return $NotesArray;
    }
	
	// Hämtar Admin-notes.
	public function getAdminNotes()
    {
        $NotesArray = array();
        
        $sqlStatement = "SELECT nid, notename, notecontent, notedatetime FROM ".$this->m_tablePrefix."notes WHERE uid=1 ORDER BY nid DESC";

        if ($stmt = $this->m_database->PrepareStatement($sqlStatement))
		{

			$stmt->execute();
		
			$stmt->bind_result($nid, $notename, $notecontent, $notedatetime);
		
			while ($stmt->fetch())
			{
				$note = new Note($nid, $notename, $notecontent, $notedatetime);
				$NotesArray[] = $note;
			}
		
			$stmt->close();
		}
		
		return $NotesArray;;
    }
	
	// Hämtar en enskild note baserat på noteId.
	public function getNote($noteId)
	{
		$SqlStatement = "SELECT uid, notename, notecontent, notedatetime FROM ".$this->m_tablePrefix."notes WHERE nid=?";
		
		$stmt = $this->m_database->PrepareStatement($SqlStatement);
		
		$stmt->bind_param("i", $noteId);
		
		$stmt->execute();
		
		$stmt->bind_result($nid, $notename, $notecontent, $notedatetime);
		
		if ($stmt->fetch() == true)
		{
			$stmt->close();
			return $note = new Note($nid, $notename, $notecontent, $notedatetime);
		}
		
		return false;
	}
	
	// #### UPDATE METODER #### //
	
	// Uppdatera en note i databasen.
	public function updateNote($nid, $notename, $notecontent, $notedatetime)
	{
		$SqlStatement = "UPDATE ".$this->m_tablePrefix."notes SET notename=?, notecontent=?, notedatetime=? WHERE nid=?";
		
		if ($stmt = $this->m_database->PrepareStatement($SqlStatement))
		{		
			$stmt->bind_param("sssi", $notename, $notecontent, $notedatetime, $nid);
			
			if ($stmt->execute())
			{
				$stmt->close();
				return true;
			}
		}
		return false;
	}
	
	// #### DELETE METODER #### //
	
	// Tar bort användarens alla notes.
	public function removeAllNotes($userId)
	{
		$sqlStatement = "DELETE FROM ".$this->m_tablePrefix."notes WHERE uid=?";

		if ($stmt = $this->m_database->PrepareStatement($sqlStatement))
		{
			$stmt->bind_param("i", $userId);
	
			if ($stmt->execute())
			{
				$stmt->close();
				return true;
			}
		}
		return false;
	}
	
	// Tar bort en enskild note
	public function removeNote($noteId)
	{
		$sqlStatement = "DELETE FROM ".$this->m_tablePrefix."notes WHERE nid=?";

		if ($stmt = $this->m_database->PrepareStatement($sqlStatement))
		{	
			$stmt->bind_param("i", $noteId);
	
			if ($stmt->execute())
			{
				return true;	
				$stmt->close();
			}
		}
		return false;
	}
	
	// #### KONTROLL FUNKTIONER #### //
	
	// Kollar om NoteId finns i databasen.
	public function doesNoteExist($noteId)
	{
	   $SqlStatement = "SELECT nid FROM ".$this->m_tablePrefix."notes WHERE nid=?";

	   $stmt = $this->m_database->PrepareStatement($SqlStatement);
	
	   // Binder inparametrarna.
	   $stmt->bind_param("i", $noteId);
	
	   // Kör SQL-frågan.
	   $stmt->execute();
	
	   // Binder resultatet.
	   $stmt->bind_result($noteId);
	
	   // Kontrollerar om användaren finns.
	   if ($stmt->fetch())
	   {
		   $stmt->close();
		   return true;
	   }
	   return false;
	}
	
	public function hasNotes($userId)
    {
       $SqlStatement = "SELECT nid FROM ".$this->m_tablePrefix."notes WHERE uid=?";
	   
	   if ($stmt = $this->m_database->PrepareStatement($SqlStatement))
	   {
		   // Binder inparametrarna.
		   $stmt->bind_param("i", $userId);
	
		   // Kör SQL-frågan.
		   $stmt->execute();
	
		   // Binder resultatet.
		   $stmt->bind_result($nid);
	
		   // Kontrollerar om användaren finns.
		   if ($stmt->fetch())
		   {
			   $stmt->close();
			   return true;
		   }
	   }
	   return false;
    }
	
	public function truncateNotesTable()
	{
		$query = "TRUNCATE TABLE test_notes";
		
		if ($this->m_database->query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
    public function test()
    {
		// TruncateNotesTable.
		if ($this->truncateNotesTable() == false)
		{
			echo "NotesHandler::truncateNotesTable kunde inte trunkera tabellen";
			return false;
		}
		
        // Kollar om användaren med uid 3 har några notes.
		if ($this->hasNotes(3) == true)
		{
			echo "NotesHandler::hasNotes har notes men borde inte ha det.";
			return false;
		}
		
		// Lägger till en ny note.
		if ($this->addNote("Testasdasdasdas", "testtetststsst", "010-10-26 16:57:19", 3) == false)
		{
			echo "NotesHandler::addNote fungerar inte men borde göra det.";
			return false;
		}
		
		// Finns note med id 32
		if ($this->doesNoteExist(32) == true)
		{
			echo "NotesHandler:doesNoteExist : nid 32 ska inte finnas, men finns?";
			return false;
		}
		
		// Finns note med id 1.
		if ($this->doesNoteExist(1) == false)
		{
			echo "NotesHandler:doesNoteExist : nid 1 ska finnas, men finns inte.";
			return false;
		}
		
		// Skapar en ny note.
		if ($this->addNote("Testasdasdasdasadadadaasd", "testtetststsst", "010-10-26 16:57:19", 3) == false)
		{
			echo "NotesHandler::addNote fungerar inte men borde göra det.";
			return false;
		}
		
		// Updaterar en note.
		if ($this->updateNote(1, "uppdaterad note", "uppdaterad note", "010-10-26 16:57:19") == false)
		{
			echo "NotesHandler::updateNote fungerar inte, men borde göra det.";
			return false;
		}
		
		
		// Tar bort en note med Id 1.
		if ($this->removeNote(1) == false)
		{
			echo "NotesHandler::removeNote med nid 1 ska fungera, men det gör den inte.";
			return false;
		}
		
		// Tar bort alla notes som tillhör användaren 3.
		if ($this->removeAllNotes(3) == false)
		{
			echo "NotesHandler::removeAllNotes med uid 3 ska fungera, men gör det inte!";
			return false;
		}
		
		// TruncateNotesTable.
		if ($this->truncateNotesTable() == false)
		{
			echo "NotesHandler::truncateNotesTable kunde inte trunkera tabellen";
			return false;
		}

		return true;
    }
}